package com.baiyang.server.service.base.impl;


import com.baiyang.server.model.system.User;
import com.baiyang.server.service.impl.BaseServiceImpl;
import com.baiyang.server.tools.DataTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baiyang.server.dao.base.UserIntegralDao;
import com.baiyang.server.model.base.UserIntegral;
import com.baiyang.server.service.base.UserIntegralService;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 
 * @author XVX
 */
@Service	
public class UserIntegralServiceImpl extends BaseServiceImpl<UserIntegralDao, UserIntegral> implements UserIntegralService{
    @Autowired
    private UserIntegralDao userIntegralDao;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private SimpleDateFormat sdf_day = new SimpleDateFormat("yyyy-MM-dd");
    /**
     * 签到
     * @param user
     * @return
     */
    @Override
    @Transactional
    public UserIntegral signIn(User user) {
        UserIntegral userIntegral = new UserIntegral();
        userIntegral.setUserId(user.getId());
        userIntegral = userIntegralDao.selectOne(userIntegral);
        userIntegral.setLastSignIn(sdf.format(new Date()));
        userIntegral.setIntegralNum(userIntegral.getIntegralNum() + 10 );
        userIntegralDao.updateById(userIntegral);
        return userIntegral;
    }

    /**
     * 判断今天是否可以签到
     * @param user
     * @return
     */
    @Override
    public boolean canSignIn(User user) {
        UserIntegral userIntegral = new UserIntegral();
        userIntegral.setUserId(user.getId());
        userIntegral = userIntegralDao.selectOne(userIntegral);
        String now = sdf_day.format(new Date());
        if( userIntegral.getLastSignIn() == null){
            return true;
        }
        String last = userIntegral.getLastSignIn().substring(0,10);
        if(!DataTool.isBlank(userIntegral.getLastSignIn())&&now.equals(last)){
            return false;
        }
        return true;
    }
}
